package com.lemon.smartsafety.ui.activity.fire.item.inspection;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.DataInfo;
import com.lemon.smartsafety.entity.RefreshViewEntity;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingIndexVo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingItemCountVo;
import com.lemon.smartsafety.entity.safe.company.CompanyBuildingVo;
import com.lemon.smartsafety.http.HttpBack;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.BaseMode;
import com.lemon.smartsafety.result.fire.FireInspectionItem;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseActivity;
import com.lemon.smartsafety.ui.activity.fire.FirefightingUtil;
import com.lemon.smartsafety.ui.adapter.fire.FireInspectionBuildItemAdapter;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.LogUtils;
import com.lemon.smartsafety.utils.app.ToastUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.view.ViewFireCompanyNone;
import com.lemon.smartsafety.view.list.ListRefreshView;

import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

public class FireInspectionBuildItemListActivity extends NewBaseActivity {

    @Bind(R.id.main_title)
    TextView mainTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @Bind(R.id.listView)
    ListRefreshView listView;
    @Bind(R.id.swipe_layout)
    SwipeRefreshLayout swipeLayout;
    @Bind(R.id.viewFireCompanyNone)
    ViewFireCompanyNone viewFireCompanyNone;


    private CompanyBuildingVo mCompanyBuildingVo;
    private CompanyBuildingIndexVo mCompanyBuildingIndexVo;
    private CompanyBuildingItemCountVo mCompanyBuildingItemCountVo;

    private FireInspectionBuildItemAdapter fireInspectionBuildItemAdapter;
    private List<FireInspectionItem> mList;
    private List<FireInspectionItem> fireInspectionItemList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void getBundleArgs() {
        if (getIntent().getExtras() != null) {
            mCompanyBuildingVo = (CompanyBuildingVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILD_INFO);
            mCompanyBuildingIndexVo = (CompanyBuildingIndexVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILDING_INDEX_VO);
            mCompanyBuildingItemCountVo = (CompanyBuildingItemCountVo) getIntent().getSerializableExtra(BundleKey.COMPANY_BUILDING_ITEM_COUNT);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_fire_inspection_build_item_list;
    }

    @Override
    protected void initTitleView() {
        this.setBarTitle("");
        try {
            String floor = mCompanyBuildingItemCountVo.getFloorNumber().contains("-") ? mCompanyBuildingItemCountVo.getFloorNumber().replace("-", "负") : mCompanyBuildingItemCountVo.getFloorNumber();
            LogUtils.LOGE("floor:" + mCompanyBuildingItemCountVo.getFloorNumber() + "     transfloor:" + floor);
            String position = mCompanyBuildingItemCountVo.getPositionTitle();
            if (CommonUtil.strIsEmpty(mCompanyBuildingItemCountVo.getPositionTitle())) {
                position = DataInfo.getChinesePosition(mCompanyBuildingItemCountVo.getPositionMark());
            }
            setSubBarTitle(mCompanyBuildingVo.getBuildingTitle() + "-" + (floor) + "层-" + position);
            setSubBarTitleColor(ContextCompat.getColor(this, R.color.white));
        } catch (Exception e) {
            e.printStackTrace();
            this.setBarTitle("检查项目");
        }

    }

    @Override
    protected void initView() {
        FirefightingUtil.fireInspectAdd(this);

        mList = new ArrayList<>();
        fireInspectionItemList = new ArrayList<>();
        fireInspectionBuildItemAdapter = new FireInspectionBuildItemAdapter(mActivity, mList
        );
        listView.setAdapter(fireInspectionBuildItemAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                try {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(BundleKey.COMPANY_BUILD_INFO, mCompanyBuildingVo);
                    bundle.putSerializable(BundleKey.COMPANY_BUILDING_ITEM_COUNT, mCompanyBuildingItemCountVo);
                    bundle.putSerializable(BundleKey.COMPANY_BUILDING_INDEX_VO, mCompanyBuildingIndexVo);
                    bundle.putSerializable(BundleKey.FIRE_INSPECTION_BUILD_ITEM_DATA, mList.get(position));
                    UIHelper.showActivity(mActivity, bundle, FireInspectionBuildItemProjectActivity.class);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        initSwipRefresh(swipeLayout);
        initLoadeRefresh(listView);
    }

    @Override
    protected void initHttp() {
        initList();
    }

    @Override
    protected void initList() {
        listView.setIsload(true);
        current_page = 1;
        httpList();
    }

    @Override
    protected void loadList() {
        if (mList.size() % page_size == 0) {
            current_page = current_page + 1;
            httpList();
        } else {
            showUpLoad(false, listView);
            listView.setIsload(false);
            ToastUtil.show("没有更多数据了");
        }
    }

    @Override
    protected void refreshView() {

    }

    /**
     * 错误的情况下 刷新
     */
    public void errorView(String title) {

        try {
            if (CommonUtil.listIsEmpty(mList)) {
                viewFireCompanyNone.setVisibility(View.VISIBLE);
                showSwipLayout(swipeLayout,false);
                if (CommonUtil.strIsEmpty(title)) {
                    viewFireCompanyNone.setContent("没有更多数据");
                    viewFireCompanyNone.setSearch("重试");
                } else {
                    viewFireCompanyNone.setContent(title);
                    viewFireCompanyNone.setSearch("重试");
                }
                viewFireCompanyNone.setAllClick(new ViewFireCompanyNone.AllClick() {
                    @Override
                    public void click() {
                        initList();
                    }
                });
            } else {
                viewFireCompanyNone.setVisibility(View.GONE);
                showSwipLayout(swipeLayout,true);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void callFinish() {
        mActivity.finish();
    }



    @Subscribe
    public void onEventMainThread(RefreshViewEntity refreshViewEntity) {
        //刷新View
        if (refreshViewEntity.getTAG().equals(FireInspectionBuildItemProjectActivity.class.getName())) {
            LogUtils.LOGE("刷新View");
            initList();
        }

    }




    public void httpList() {
        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("building_id", mCompanyBuildingVo.getBuildingId());
        params.put("building_id", mCompanyBuildingItemCountVo.getBuildingId());
        params.put("floor_number", mCompanyBuildingItemCountVo.getFloorNumber());
        params.put("position_mark", mCompanyBuildingItemCountVo.getPositionMark());

        showLoading("");
        HttpOkBase.httpBasePost(params, HttpPath.getmBase(HttpPath.Get_company_building_floor_item), "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                dismissLoading();
                if (httpClient) {
                    BaseMode baseMode = JsonUtils.fromJson(result, BaseMode.class);
                    if (!CommonUtil.objIsEmpty(baseMode)) {
                        if (HttpBack.httpBackError(baseMode)) {
                            fireInspectionItemList = mGson.fromJson(mGson.toJson(baseMode.getResult()), new TypeToken<List<FireInspectionItem>>() {
                            }.getType());
                            transList();

                        } else {
                            errorView(baseMode.getMsg());
                            ToastUtil.show(baseMode.getMsg());
                        }
                    }
                }else{
                    fireInspectionItemList=null;
                    errorView("网络连接失败");
                }
            }
        });

    }


    /**
     * 转换list
     */
    private void transList() {
        try {
            showUpLoad(false, listView);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (current_page == 1) {
            mList.clear();
        }
        mList.addAll(fireInspectionItemList);
        fireInspectionBuildItemAdapter.notifyDataSetChanged();

        errorView("");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean menuResult = super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu_fire_close, menu);
        return menuResult;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_fire_close) {
            //点击了关闭检查
            LogUtils.LOGE("点击了关闭检查");
            FirefightingUtil.fireColose(mActivity);

        }
        return super.onOptionsItemSelected(item);
    }


}
